package com.dt.platform.hr.page;

import com.alibaba.fastjson.JSONObject;
import com.dt.platform.constants.enums.common.StatusEnableEnum;
import com.dt.platform.constants.enums.hr.LearnTaskStatusEnum;
import com.dt.platform.constants.enums.hr.PaperTaskResultStatusEnum;
import com.dt.platform.constants.enums.hr.PaperTaskStatusEnum;
import com.dt.platform.domain.hr.*;
import com.dt.platform.proxy.hr.LearnServiceProxy;
import com.dt.platform.proxy.hr.LearnTaskServiceProxy;
import com.dt.platform.proxy.hr.PaperTaskServiceProxy;
import com.github.foxnic.api.transter.Result;
import org.github.foxnic.web.framework.view.controller.ViewController;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import com.dt.platform.proxy.hr.PaperServiceProxy;
import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * <p>
 * 考试试卷模版页面控制器
 * </p>
 * @author 金杰 , maillank@qq.com
 * @since 2024-09-13 09:16:52
*/

@Controller("HrPaperPageController")
@RequestMapping(PaperPageController.prefix)
public class PaperPageController extends ViewController {

	public static final String prefix="business/hr/paper";

	private PaperServiceProxy proxy;

	/**
	 * 获得代理对象<br>
	 * 1、单体应用时，在应用内部调用；<br>
	 * 2、前后端分离时，通过配置，以Rest方式调用后端；<br>
	 * 3、微服务时，通过feign调用; <br>
	 * */
	public PaperServiceProxy proxy() {
		if(proxy==null) {
			proxy=PaperServiceProxy.api();
		}
		return proxy;
	}

	/**
	 * 学习课程 功能主页面
	 */
	@RequestMapping("/paper_do.html")
	public String paperDo(Model model,HttpServletRequest request,String role,String paperId,String taskId) {

		boolean result=true;
		double paperScore=0.0;
		model.addAttribute("paperId",paperId);
		model.addAttribute("taskId",taskId);

		//review,task
		if("review".equals(role)){
			//试卷预览
			Result<JSONObject> paperRes= PaperServiceProxy.api().getPaper(paperId,taskId);
			if(!paperRes.isSuccess()){
				model.addAttribute("msg",paperRes.getMessage());
				result=false;
				model.addAttribute("result",result);
				return getTemplatePath(prefix,"paper_do");
			}
			model.addAttribute("paperData",paperRes.getData());
		}else if("task".equals(role)){
			Result<PaperTask> taskRes= PaperTaskServiceProxy.api().getById(taskId);

			//从任务单进入
			PaperTask task=null;
			if(taskRes.isSuccess()){
				task=taskRes.getData();
				model.addAttribute("taskData",taskRes.getData());
			}else{
				result=false;
				model.addAttribute("result",result);
				model.addAttribute("msg","没有获取到任务单");
				return getTemplatePath(prefix,"paper_do");
			}

			PaperRelease release=task.getPaperRelease();
			//判断当前是可以进入学习
			if(StatusEnableEnum.DISABLE.code().equals(release.getStatus())){
				model.addAttribute("msg","当前考试任务未启用，请联系管理员进行操作!");
				result=false;
				model.addAttribute("result",result);
				return getTemplatePath(prefix,"paper_do");
			}
			//是否已经作废
			if(task.getStatus().equals(PaperTaskStatusEnum.CANCEL.code())){
				model.addAttribute("msg","当前考试任务已作废");
				result=false;
				model.addAttribute("result",result);
				return getTemplatePath(prefix,"paper_do");
			}

			//判断当前时间是否到
			Date curTime=new Date();
			if(curTime.before(release.getSdate())){
				String format="yyyy-MM-dd HH:mm:ss";
				SimpleDateFormat sdf=new SimpleDateFormat(format);
				model.addAttribute("msg","当前考试任务时间未到，开始时间为:"+sdf.format(release.getSdate()));
				result=false;
				model.addAttribute("result",result);
				return getTemplatePath(prefix,"paper_do");
			}

			//未答题，首次进入答题开始试卷
			if(PaperTaskResultStatusEnum.NOT_ACT.code().equals(task.getPaperStatus())){
				if(curTime.after(release.getEdate())){
					String format="yyyy-MM-dd HH:mm:ss";
					SimpleDateFormat sdf=new SimpleDateFormat(format);
					model.addAttribute("msg","当前考试任务时间已过，截止时间为:"+sdf.format(release.getSdate()));
					result=false;
					model.addAttribute("result",result);
					return getTemplatePath(prefix,"paper_do");
				}

//				//第一次打开学习任务，插入开始时间
//				if(task.getStartTime()==null){
//					//插入一条数据
//					PaperTaskVO taskUpdate=PaperTaskVO.create();
//					taskUpdate.setStartTime(new Date());
//					taskUpdate.setId(taskId);
//					taskUpdate.setPaperStatus(PaperTaskResultStatusEnum.ACTING.code());
//					PaperTaskServiceProxy.api().update(taskUpdate);
//				}
				//插入一条数据
				PaperTaskVO taskUpdate=PaperTaskVO.create();
				taskUpdate.setStartTime(new Date());
				taskUpdate.setId(taskId);
				taskUpdate.setPaperStatus(PaperTaskResultStatusEnum.ACTING.code());
				PaperTaskServiceProxy.api().update(taskUpdate);
			}


			//已经答题，进入显示页面
			if(PaperTaskResultStatusEnum.PASS.code().equals(task.getPaperStatus())
			||PaperTaskResultStatusEnum.FAIL.code().equals(task.getPaperStatus())
			||PaperTaskResultStatusEnum.ACTED.code().equals(task.getPaperStatus())){
				role="taskview";
			}
			Result<JSONObject> paperRes= PaperServiceProxy.api().getPaper(task.getPaperId(),taskId);
			model.addAttribute("paperData",paperRes.getData());

		}

		model.addAttribute("paperScore",paperScore);
		model.addAttribute("role",role);
		model.addAttribute("result",result);
		return getTemplatePath(prefix,"paper_do");
	}


	/**
	 * 考试试卷 功能主页面
	 */
	@RequestMapping("/paper_list.html")
	public String list(Model model,HttpServletRequest request) {
		return getTemplatePath(prefix,"paper_list");
	}

	/**
	 * 考试试卷 表单页面
	 */
	@RequestMapping("/paper_form.html")
	public String form(Model model,HttpServletRequest request , String id) {
		return getTemplatePath(prefix,"paper_form");
	}
}